package javapop.framework.parser;

import javapop.framework.ParseContext;
import javapop.framework.ParseError;
import javapop.framework.ParseInput;
import javapop.framework.ParseResult;
import javapop.framework.Parser;

public class Forward<T> extends Parser<T> {
	private Parser<T> child;
	
	public Forward(Parser<T> child) {
		if(child==null) {
			throw new IllegalArgumentException("Child parser is <null>");
		}
		this.child = child;
	}
	
	@Override
	public String getDescription() {
		return child.getDescription();
	}
	
	@Override
	public boolean isMemoized() {
		return false;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public ParseResult<T> eval(ParseContext<?> ctx, ParseInput input) {
		int savePos = input.getPos();
		ParseResult<T> result = child.parse(ctx,input);
		if(result.isError()) {
			return (ParseResult) result.asError();
		}
		if(input.getPos()==savePos) {
			return (ParseResult) new ParseError("Expected some input to be consumed");
		}
		
		
		return result;
	}

}
